package com.hhkj.study.admin.user.service.extend.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.hhkj.study.admin.user.entity.UserLogin;
import com.hhkj.study.admin.user.model.podo.UserLoginDo;
import com.hhkj.study.admin.user.model.podo.UserLoginPo;
import com.hhkj.study.admin.user.service.base.UserLoginService;
import com.hhkj.study.admin.user.service.extend.UserLoginExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class UserLoginExtendServiceImpl extends BaseExtendServiceImpl<UserLogin, UserLoginDo, UserLoginPo, UserLoginService> implements UserLoginExtendService {
    @Override
    public Map<Long, Date> getLastLoginTimeMap(List<Long> userIdList) {
        LambdaQueryWrapper<UserLogin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserLogin::getUserId, userIdList).select(UserLogin::getUserId, UserLogin::getCreateTime).orderByDesc(UserLogin::getCreateTime).last("LIMIT 1");
        List<UserLogin> userLoginList = this.getService().list(queryWrapper);
        if (CollectionUtil.isEmpty(userLoginList)) {
            return Maps.newHashMap();
        }
        return userLoginList.stream().collect(Collectors.toMap(UserLogin::getUserId, UserLogin::getCreateTime, (o, v) -> v));
    }
}
